package com.kehutong.tag.admin;

import com.kehutong.common.entity.Root;
import com.kehutong.common.util.Token;
import com.kehutong.crm.entity.Tag;
import com.kehutong.crm.enums.TagSourceType;
import com.kehutong.tag.entity.CustomizeTag;
import com.kehutong.tag.entity.TemplateTag;
import com.kehutong.tag.entity.UserTag;
import com.kehutong.tag.service.CustomizeTagService;
import com.kehutong.tag.service.TemplateTagService;
import com.kehutong.tag.service.ThirdPartyTagService;
import com.kehutong.tag.service.UserTagService;
import org.coraframework.authz.HasPermission;
import org.coraframework.bean.Beans;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.EnumUtil;
import org.coraframework.util.Objects;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.*;

/**
 * 住户标签接口
 *
 * @author: liukunlong
 * @date: 2021-08-09 10:48
 */
@MvcAction
@ReqMapping("/admin/crm/user/tag")
public class UserTagController {
    static final Logger logger = LoggerFactory.getLogger(UserTagController.class);

    @Inject
    private UserTagService userTagService;

    @Inject
    private CustomizeTagService customizeTagService;

    @Inject
    private TemplateTagService templateTagService;

    @Inject
    private ThirdPartyTagService thirdPartyTagService;


    @Inject
    private JdbcSession jdbcSession;



    /**
     * 根据条件获取标签列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @ReqMapping("/list")
    public Object list(Token token,JSONObject jsonObject) throws Exception {
        return userTagService.getUserTagList(jsonObject);
    }

    /**
     * 用户自定义标签
     *
     * @param token
     * @param jsonObject
     * @return
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(Token token, JSONObject jsonObject) {
        //1.模板标签 2.自定义标签 3.业务标签 4.第三方标签
        String tagType = jsonObject.getString("tagType");
        String pid = jsonObject.getString("pid");
        String name = jsonObject.getString("name");
        //如果是模板标签先查看自定义标签中是否有这个标签组,如果没有就创建
        if (Objects.equal(tagType, "1")) {
            CustomizeTag dbCustomizeTag = jdbcSession.findOne(CustomizeTag.class)
                    .eq("deleted", false)
                    .eq("id", pid).exe();
            if (Objects.isNull(dbCustomizeTag)) {
                TemplateTag dbTemplateTag = jdbcSession.findOne(TemplateTag.class)
                        .eq("deleted", false)
                        .eq("id", pid).exe();
                CustomizeTag customizeTagGroup = Pool.newInstance(CustomizeTag.class, dbTemplateTag.getId());
                customizeTagGroup.setPid(Root.ROOT_ID);
                customizeTagGroup.setName(dbTemplateTag.getName());
                customizeTagGroup.setGroup(true);
                customizeTagGroup.setCreateBy(token.getUuid());
                customizeTagGroup.setCreateName(token.getUsername());
                jdbcSession.insert(customizeTagGroup);
            }else{
                //如果创建的标签已经存在则更新标签信息,增加共创人信息
                List<String> commonCreateBy = dbCustomizeTag.getCommonCreateBy();
                List<String> commonCreateName = dbCustomizeTag.getCommonCreateName();
                if (Objects.isEmpty(commonCreateBy)) {
                    commonCreateBy = new ArrayList<>();
                    commonCreateName = new ArrayList<>();
                }
                if(!commonCreateBy.contains(token.getUuid())){
                    commonCreateBy.add(token.getUuid());
                    commonCreateName.add(token.getUsername());
                    dbCustomizeTag.setCommonCreateBy(commonCreateBy);
                    dbCustomizeTag.setCommonCreateName(commonCreateName);
                }
                jdbcSession.updateById(dbCustomizeTag);
            }

        }
        //创建自定义标签
        CustomizeTag customizeTag = Pool.newInstance(CustomizeTag.class, UUIDGenerator.randomUUID());
        customizeTag.setPid(pid);
        customizeTag.setName(name);
        customizeTag.setCreateBy(token.getUuid());
        customizeTag.setCreateName(token.getUsername());
        customizeTagService.save(customizeTag);
        return Result.success();
    }


    /**
     * 用户自定义多个标签
     *
     * @param token
     * @param jsonObject
     * @return
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/multiple/save")
    public Object multipleSave(Token token, JSONObject jsonObject) {
        //1.模板标签 2.自定义标签 3.业务标签 4.第三方标签
        String tagType = jsonObject.getString("tagType");
        String pid = jsonObject.getString("pid");
        String name = jsonObject.getString("name");
        //如果是模板标签先查看自定义标签中是否有这个标签组,如果没有就创建
        if (Objects.equal(tagType, "1")) {
            CustomizeTag dbCustomizeTag = jdbcSession.findOne(CustomizeTag.class)
                    .eq("deleted", false)
                    .eq("id", pid).exe();
            if (Objects.isNull(dbCustomizeTag)) {
                TemplateTag dbTemplateTag = jdbcSession.findOne(TemplateTag.class)
                        .eq("deleted", false)
                        .eq("id", pid).exe();
                CustomizeTag customizeTagGroup = Pool.newInstance(CustomizeTag.class, dbTemplateTag.getId());
                customizeTagGroup.setPid(Root.ROOT_ID);
                customizeTagGroup.setName(dbTemplateTag.getName());
                customizeTagGroup.setGroup(true);
                customizeTagGroup.setCreateBy(token.getUuid());
                customizeTagGroup.setCreateName(token.getUsername());
                jdbcSession.insert(customizeTagGroup);
            }else{
                //如果创建的标签已经存在则更新标签信息,增加共创人信息
                List<String> commonCreateBy = dbCustomizeTag.getCommonCreateBy();
                List<String> commonCreateName = dbCustomizeTag.getCommonCreateName();
                if (Objects.isEmpty(commonCreateBy)) {
                    commonCreateBy = new ArrayList<>();
                    commonCreateName = new ArrayList<>();
                }
                if(!commonCreateBy.contains(token.getUuid())){
                    commonCreateBy.add(token.getUuid());
                    commonCreateName.add(token.getUsername());
                    dbCustomizeTag.setCommonCreateBy(commonCreateBy);
                    dbCustomizeTag.setCommonCreateName(commonCreateName);
                }
                jdbcSession.updateById(dbCustomizeTag);
            }

        }
        //创建自定义标签
        if(Objects.nonEmpty(name)){
            String[] tagName=name.split(",");
            for (String tag:tagName){
                CustomizeTag customizeTag = Pool.newInstance(CustomizeTag.class, UUIDGenerator.randomUUID());
                customizeTag.setPid(pid);
                customizeTag.setName(tag);
                customizeTag.setCreateBy(token.getUuid());
                customizeTag.setCreateName(token.getUsername());
                customizeTagService.save(customizeTag);
            }
        }
        return Result.success();
    }

    /**
     * 根据条件获取当前员工可以看到的自定义标签组
     *
     * @param jsonObject 查询对象
     * @return 自定义标签组列表
     * @throws IOException 操作异常
     */
    @ReqMapping("/customize/group/list")
    public Object customizeGroupList(Token token,JSONObject jsonObject) throws Exception {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        jsonObject.put("createBy",token.getUuid());
        return customizeTagService.groupList(jsonObject);
    }


    /**
     * 用户自定义标签组新增
     *
     * @param token
     * @param jsonObject
     * @return
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/group/save")
    public Object groupSave(Token token, JSONObject jsonObject) {
        if (Objects.isNull(token)) {
            return Result.unlogin();
        }
        String name = jsonObject.getString("name");
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        CustomizeTag customizeTagGroup = Pool.newInstance(CustomizeTag.class, UUIDGenerator.randomUUID());
        customizeTagGroup.setPid(Root.ROOT_ID);
        customizeTagGroup.setGroup(true);
        customizeTagGroup.setName(name);
        customizeTagGroup.setCreateBy(token.getUuid());
        customizeTagGroup.setCreateName(token.getUsername());
        customizeTagGroup.setSort(1);
        List<CustomizeTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), CustomizeTag.class);
        customizeTagGroup.setChildList(childList);
        return customizeTagService.groupSave(customizeTagGroup);
    }


    /**
     * 用户标签标记
     * 先删除在重新标记标签
     * tabType 1.用户标签(模板标签、自定义标签、业务标签、舆情标签、第三方标签)如不同来源的标签组和标签有重复的，则需去重合并
     * tabType 2.全部用户标签(模板标签和自定义标签)，自己给客户添加的标签、非自己添加的标签、可添加标签，显示3种不同颜色
     * tabType 3.个人标签(仅限员工自己创建和共创的自定义标签)
     * tabType 4.其他标签(舆情标签、业务标签、第三方标签)自己添加的标签和非自己添加的标签以不同颜色区分
     * @param jsonObject
     * @return
     */
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/make")
    public Object make(Token token, JSONObject jsonObject) {
        String userNo = jsonObject.getString("userNo");
        String userName = jsonObject.getString("userName");
        //1.用户标签 2.全部用户标签  3.个人标签 4.其他标签
        String tabType = jsonObject.getString("tabType");
        String communityNo = jsonObject.getString("communityNo");
        String communityName = jsonObject.getString("communityName");
        JSONArray userTagListJsonArray = jsonObject.getJSONArray("userTagList");
        if (Objects.nonEmpty(userTagListJsonArray)) {
            JSONObject condition = new JSONObject();
            condition.put("userNo", userNo);
            condition.put("createBy", token.getUuid());
            condition.put("communityNo", communityNo);
            if(Objects.equal("2",tabType)){
                JSONArray typeArray=new JSONArray();
                typeArray.add("1");
                typeArray.add("2");
                condition.put("type", typeArray);
            }else if(Objects.equal("3",tabType)){
                JSONArray typeArray=new JSONArray();
                typeArray.add("2");
                condition.put("type", typeArray);
            }else{
                return Result.success();
            }
            List<UserTag> deleteTagList = jdbcSession.findArray(UserTag.class)
                    .eq(condition, "userNo", "createBy", "communityNo").in(condition,"type").exe();
            jdbcSession.deleteById(deleteTagList);
            List<UserTag> userTagList = new ArrayList<>();
            for (Object tagObj : userTagListJsonArray) {
                JSONObject tag = (JSONObject) tagObj;
                String type=tag.getString("type");
                tag.remove("type");
                UserTag userTag = Pool.newInstance(UserTag.class, UUIDGenerator.randomUUID());
                Beans.from(tag).to(userTag).exclude("id").igornNull().copy();
                userTag.setUserNo(userNo);
                userTag.setUserName(userName);
                userTag.setTagGroupNo(tag.getString("pid"));
                userTag.setTagGroupName(tag.getString("pname"));
                userTag.setTagNo(tag.getString("id"));
                userTag.setTagName(tag.getString("name"));
                userTag.setCommunityNo(communityNo);
                userTag.setCommunityName(communityName);
                userTag.setCreateBy(token.getUuid());
                userTag.setCreateName(token.getUsername());
                //设置用户标签来源,传入枚举转化报错
                if(Objects.nonNull(type)){
                    userTag.setType(EnumUtil.getEnum(TagSourceType.class, type));
                }
                userTagList.add(userTag);
            }
            jdbcSession.insert(userTagList);
        }
        return Result.success();
    }

    /**
     * 标签使用用户分页列表
     *
     * @param jsonObject 查询对象
     * @return 标签使用用户分页列表
     * @throws IOException 操作异常
     */
    @ReqMapping("/page")
    public Page<UserTag> userPage(JSONObject jsonObject) throws Exception {
        return userTagService.page(jsonObject);
    }

    /**
     * 用户标签列表
     * type 1.用户标签(模板标签、自定义标签、业务标签、舆情标签、第三方标签)如不同来源的标签组和标签有重复的，则需去重合并
     * type 2.全部用户标签(模板标签和自定义标签)，自己给客户添加的标签、非自己添加的标签、可添加标签，显示3种不同颜色
     * type 3.个人标签(仅限员工自己创建和共创的自定义标签)
     * type 4.其他标签(舆情标签、业务标签、第三方标签)自己添加的标签和非自己添加的标签以不同颜色区分
     *
     * @param jsonObject
     * @return
     * @throws Exception
     */
    @ReqMapping("/group/list")
    public Object groupList(Token token, JSONObject jsonObject) throws Exception {
        String userNo = jsonObject.getString("userNo");
        String type = jsonObject.getString("type");
        if (Objects.equal("1", type)) {
            jsonObject.remove("type");
            List<Map<String, Object>> userTagGroupListMap = userTagService.getUserTagGroupList(jsonObject);
            for (Map<String, Object> userTagGroupMap : userTagGroupListMap) {
                String tagGroupName = (String) userTagGroupMap.get("name");
                jsonObject.put("tagGroupName", tagGroupName);
                List<Map<String, Object>> userTagListMap = userTagService.getUserTagList(jsonObject);
                userTagGroupMap.put("childList", userTagListMap);
            }
            return userTagGroupListMap;
        } else if (Objects.equal("2", type)) {
             return getAllUserTagMarkList(token, jsonObject);
        } else if (Objects.equal("3", type)) {
            return getCustomizeUserTagMarkList(token,jsonObject);
        } else if (Objects.equal("4", type)) {
            //其他标签来源类型条件(舆情标签、业务标签、第三方标签)
            JSONArray typeArray=new JSONArray();
            typeArray.add("3");
            typeArray.add("4");
            typeArray.add("5");
            jsonObject.put("type",typeArray);

            //查询其他合并后标签组
            List<Map<String, Object>> userTagGroupListMap = userTagService.getUserTagGroupList(jsonObject);
            for (Map<String, Object> userTagGroupMap : userTagGroupListMap) {
                //查询其他标签组下面合并后的标签
                String tagGroupName = (String) userTagGroupMap.get("name");
                jsonObject.put("tagGroupName", tagGroupName);
                List<Map<String, Object>> userTagListMap = userTagService.getUserTagList(jsonObject);
                if (Objects.nonEmpty(userTagListMap)) {
                    jsonObject.put("createBy", token.getUuid());
                    for (Map<String, Object> tagMap : userTagListMap) {
                        //遍历标签设置自己添加的标签和非自己添加的标签 1非自己添加 2.自己添加
                        String tagName = (String) tagMap.get("name");
                        jsonObject.put("tagName", tagName);
                        List<UserTag> userTagList = jdbcSession.findArray(UserTag.class)
                                .eq(jsonObject, "userNo", "communityNo","tagName").exe();
                        if(Objects.nonEmpty(userTagList)){
                            tagMap.put("markStatus", "2");
                        }else {
                            tagMap.put("markStatus", "1");
                        }
                    }
                }


                userTagGroupMap.put("childList", userTagListMap);
            }
            return userTagGroupListMap;
        } else {

        }
        return getUserTagList(token, jsonObject);
    }

    /**
     * 获取全部用户标签(模板标签和自定义标签)，自己给客户添加的标签、非自己添加的标签、可添加标签，显示3种不同颜色
     *
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     */
    private Object makeList(Token token, JSONObject jsonObject) throws Exception {
        List<JSONObject> tagGroupList = getUserTagList(token, jsonObject);

        if (Objects.nonEmpty(tagGroupList)) {
            jsonObject.put("createBy", token.getUuid());
            List<UserTag> userTagList = jdbcSession.findArray(UserTag.class)
                    .eq(jsonObject, "userNo", "communityNo").exe();

            if (Objects.isEmpty(userTagList)) {
                return tagGroupList;
            }

            //标签与标记员工编号映射
            Map<String, String> makeMapping = new HashMap<>();

            userTagList.forEach(v -> {
                makeMapping.put(v.getTagNo(), v.getCreateBy());
            });
            for (Object tagGroupObj : tagGroupList) {
                JSONObject tagGroup = (JSONObject) tagGroupObj;
                JSONArray childList = tagGroup.getJSONArray("childList");
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagId = tag.getString("id");
                        //设置标签的标记状态 0.没有被标记过 1.被其他人标记的 2.自己标记的
                        int markStatus = 0;
                        if (makeMapping.containsKey(tagId)) {
                            String createBy = makeMapping.get(tagId);
                            if (Objects.equal(createBy, token.getUuid())) {
                                markStatus = 2;
                            } else {
                                markStatus = 1;
                            }
                        }
                        tag.put("markStatus", markStatus);
                    }
                }
            }
        }
        return tagGroupList;
    }

    /**
     * 获取个人用户标签标记的结果
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     */
    private Object getCustomizeUserTagMarkList(Token token, JSONObject jsonObject) throws Exception {
        List<JSONObject> tagGroupList = getCustomizeUserTagList(token, jsonObject);

        if (Objects.nonEmpty(tagGroupList)) {
            jsonObject.put("createBy", token.getUuid());
            List<UserTag> userTagList = jdbcSession.findArray(UserTag.class)
                    .eq(jsonObject, "userNo", "communityNo").exe();

            if (Objects.isEmpty(userTagList)) {
                return tagGroupList;
            }

            //标签与标记员工编号映射
            Map<String, String> makeMapping = new HashMap<>();

            userTagList.forEach(v -> {
                makeMapping.put(v.getTagNo(), v.getCreateBy());
            });
            for (Object tagGroupObj : tagGroupList) {
                JSONObject tagGroup = (JSONObject) tagGroupObj;
                JSONArray childList = tagGroup.getJSONArray("childList");
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagId = tag.getString("id");
                        //设置标签的标记状态 0.没有被标记过 1.被其他人标记的 2.自己标记的
                        int markStatus = 0;
                        if (makeMapping.containsKey(tagId)) {
                            String createBy = makeMapping.get(tagId);
                            if (Objects.equal(createBy, token.getUuid())) {
                                markStatus = 2;
                            } else {
                                markStatus = 1;
                            }
                        }
                        tag.put("markStatus", markStatus);
                    }
                }
            }
        }
        return tagGroupList;
    }


    /**
     * 获取自定义标签显示数据接口
     *
     * @param jsonObject
     * @return
     */
    private List<JSONObject> getCustomizeUserTagList(Token token, JSONObject jsonObject) throws Exception {
        //1.模板标签 2.自定义标签 3.业务标签 4.第三方标签
        //模板标签组名称与模板标签组的映射关系
        Map<String, JSONObject> tagGroupMapping = new LinkedHashMap<>();
        //过滤自己能看到的自定义标签数据(自己创建的,共创人是自己的)
        JSONObject customizeTagGroupCondition = new JSONObject();
        customizeTagGroupCondition.put("createBy", token.getUuid());
        List<CustomizeTag> customizeTagGroupList = (List<CustomizeTag>) customizeTagService.groupList(customizeTagGroupCondition);
        for (CustomizeTag customizeTagGroup : customizeTagGroupList) {
            JSONObject customizeTagGroupJson = (JSONObject) JSONObject.toJSON(customizeTagGroup);
            customizeTagGroupJson.put("type", TagSourceType.CUSTOMIZE_TAG.getId());
            tagGroupMapping.put(customizeTagGroup.getName(), customizeTagGroupJson);
        }

        List<JSONObject> tagGroupList = new ArrayList<>();
        tagGroupMapping.forEach((key, value) -> {
            tagGroupList.add(value);
        });
        return tagGroupList;
    }


    /**
     * 获取全部用户标签标记的结果(模板标签和自定义标签)，自己给客户添加的标签、非自己添加的标签、可添加标签，显示3种不同颜色
     *
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     */
    private Object getAllUserTagMarkList(Token token, JSONObject jsonObject) throws Exception {
        List<JSONObject> tagGroupList = getAllUserTagList(token, jsonObject);

        if (Objects.nonEmpty(tagGroupList)) {
            jsonObject.put("createBy", token.getUuid());
            List<UserTag> userTagList = jdbcSession.findArray(UserTag.class)
                    .eq(jsonObject, "userNo", "communityNo").exe();

            if (Objects.isEmpty(userTagList)) {
                return tagGroupList;
            }

            //标签与标记员工编号映射
            Map<String, String> makeMapping = new HashMap<>();

            userTagList.forEach(v -> {
                makeMapping.put(v.getTagNo(), v.getCreateBy());
            });
            for (Object tagGroupObj : tagGroupList) {
                JSONObject tagGroup = (JSONObject) tagGroupObj;
                JSONArray childList = tagGroup.getJSONArray("childList");
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagId = tag.getString("id");
                        //设置标签的标记状态 0.没有被标记过 1.被其他人标记的 2.自己标记的
                        int markStatus = 0;
                        if (makeMapping.containsKey(tagId)) {
                            String createBy = makeMapping.get(tagId);
                            if (Objects.equal(createBy, token.getUuid())) {
                                markStatus = 2;
                            } else {
                                markStatus = 1;
                            }
                        }
                        tag.put("markStatus", markStatus);
                    }
                }
            }
        }
        return tagGroupList;
    }

    /**
     * 全部用户标签显示数据接口
     *
     * @param jsonObject
     * @return
     */
    private List<JSONObject> getAllUserTagList(Token token, JSONObject jsonObject) throws Exception {
        //1.模板标签 2.自定义标签 3.业务标签 4.第三方标签
        //模板标签组名称与模板标签组的映射关系
        Map<String, JSONObject> templateTagGroupMapping = new LinkedHashMap<>();

        JSONObject templateTagGroupCondition = new JSONObject();
        //模板标签组集合
        List<TemplateTag> templateTagGroupList = (List<TemplateTag>) templateTagService.groupList(templateTagGroupCondition);
        if (Objects.nonEmpty(templateTagGroupList)) {
            for (TemplateTag templateTagGroup : templateTagGroupList) {
                JSONObject templateTagGroupJson = (JSONObject) JSONObject.toJSON(templateTagGroup);
                templateTagGroupJson.put("type", TagSourceType.TEMPLATE_TAG.getId());
                templateTagGroupMapping.put(templateTagGroup.getName(), templateTagGroupJson);
            }
        }

        //过滤自己能看到的自定义标签数据(自己创建的,共创人是自己的)
        JSONObject customizeTagGroupCondition = new JSONObject();
        customizeTagGroupCondition.put("createBy", token.getUuid());
        List<CustomizeTag> customizeTagGroupList = (List<CustomizeTag>) customizeTagService.groupList(customizeTagGroupCondition);
        for (CustomizeTag customizeTagGroup : customizeTagGroupList) {
            if (templateTagGroupMapping.containsKey(customizeTagGroup.getName())) {
                JSONObject templateTagGroupJson = templateTagGroupMapping.get(customizeTagGroup.getName());
                JSONArray childList = templateTagGroupJson.getJSONArray("childList");
                //模板标签名称与模板标签名称的映射关系
                Map<String, String> templateTagMapping = new LinkedHashMap<>();
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagName = tag.getString("name");
                        templateTagMapping.put(tagName, tagName);
                    }
                }
                //如果自定义的标签与模板的标签一致则忽略自定义标签，否则在模板标签组中追加标签
                if (Objects.nonEmpty(customizeTagGroup.getChildList())) {
                    customizeTagGroup.getChildList().forEach(v -> {
                        if (!templateTagMapping.containsKey(v.getName())) {
                            JSONObject customizeTagJson = (JSONObject) JSONObject.toJSON(v);
                            childList.add(customizeTagJson);
                        }
                    });
                }
            } else {
                JSONObject customizeTagGroupJson = (JSONObject) JSONObject.toJSON(customizeTagGroup);
                customizeTagGroupJson.put("type", TagSourceType.CUSTOMIZE_TAG.getId());
                templateTagGroupMapping.put(customizeTagGroup.getName(), customizeTagGroupJson);
            }

        }

        List<JSONObject> tagGroupList = new ArrayList<>();
        templateTagGroupMapping.forEach((key, value) -> {
            tagGroupList.add(value);
        });
        return tagGroupList;
    }


    /**
     * 获取全部用户标签标记的结果(模板标签和自定义标签)
     *
     * @param token
     * @param jsonObject
     * @return
     * @throws Exception
     */
    private Object getOtherUserTagMarkList(Token token, JSONObject jsonObject) throws Exception {
        List<JSONObject> tagGroupList = getOtherUserTagList(token, jsonObject);

        if (Objects.nonEmpty(tagGroupList)) {
            jsonObject.put("createBy", token.getUuid());
            List<UserTag> userTagList = jdbcSession.findArray(UserTag.class)
                    .eq(jsonObject, "userNo", "communityNo").exe();

            if (Objects.isEmpty(userTagList)) {
                return tagGroupList;
            }

            //标签与标记员工编号映射
            Map<String, String> makeMapping = new HashMap<>();

            userTagList.forEach(v -> {
                makeMapping.put(v.getTagNo(), v.getCreateBy());
            });
            for (Object tagGroupObj : tagGroupList) {
                JSONObject tagGroup = (JSONObject) tagGroupObj;
                JSONArray childList = tagGroup.getJSONArray("childList");
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagId = tag.getString("id");
                        //设置标签的标记状态 0.没有被标记过 1.被其他人标记的 2.自己标记的
                        int markStatus = 0;
                        if (makeMapping.containsKey(tagId)) {
                            String createBy = makeMapping.get(tagId);
                            if (Objects.equal(createBy, token.getUserId())) {
                                markStatus = 2;
                            } else {
                                markStatus = 1;
                            }
                        }
                        tag.put("markStatus", markStatus);
                    }
                }
            }
        }
        return tagGroupList;
    }

    /**
     * 其他用户标签显示数据接口
     *
     * @param jsonObject
     * @return
     */
    private List<JSONObject> getOtherUserTagList(Token token, JSONObject jsonObject) throws Exception {
        //1.模板标签 2.自定义标签 3.业务标签 4.第三方标签
        //模板标签组名称与模板标签组的映射关系
        Map<String, JSONObject> templateTagGroupMapping = new LinkedHashMap<>();

        JSONObject templateTagGroupCondition = new JSONObject();
        //模板标签组集合
        List<TemplateTag> templateTagGroupList = (List<TemplateTag>) templateTagService.groupList(templateTagGroupCondition);
        if (Objects.nonEmpty(templateTagGroupList)) {
            for (TemplateTag templateTagGroup : templateTagGroupList) {
                JSONObject templateTagGroupJson = (JSONObject) JSONObject.toJSON(templateTagGroup);
                templateTagGroupJson.put("type", TagSourceType.TEMPLATE_TAG.getId());
                templateTagGroupMapping.put(templateTagGroup.getName(), templateTagGroupJson);
            }
        }

        //过滤自己能看到的自定义标签数据(自己创建的,共创人是自己的)
        JSONObject customizeTagGroupCondition = new JSONObject();
        customizeTagGroupCondition.put("createBy", token.getUuid());
        List<CustomizeTag> customizeTagGroupList = (List<CustomizeTag>) customizeTagService.groupList(customizeTagGroupCondition);
        for (CustomizeTag customizeTagGroup : customizeTagGroupList) {
            if (templateTagGroupMapping.containsKey(customizeTagGroup.getName())) {
                JSONObject templateTagGroupJson = templateTagGroupMapping.get(customizeTagGroup.getName());
                JSONArray childList = templateTagGroupJson.getJSONArray("childList");
                //模板标签名称与模板标签名称的映射关系
                Map<String, String> templateTagMapping = new LinkedHashMap<>();
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagName = tag.getString("name");
                        templateTagMapping.put(tagName, tagName);
                    }
                }
                //如果自定义的标签与模板的标签一致则忽略自定义标签，否则在模板标签组中追加标签
                if (Objects.nonEmpty(customizeTagGroup.getChildList())) {
                    customizeTagGroup.getChildList().forEach(v -> {
                        if (!templateTagMapping.containsKey(v.getName())) {
                            JSONObject customizeTagJson = (JSONObject) JSONObject.toJSON(v);
                            childList.add(customizeTagJson);
                        }
                    });
                }

            } else {
                JSONObject customizeTagGroupJson = (JSONObject) JSONObject.toJSON(customizeTagGroup);
                customizeTagGroupJson.put("type", TagSourceType.CUSTOMIZE_TAG.getId());
                templateTagGroupMapping.put(customizeTagGroup.getName(), customizeTagGroupJson);
            }

        }

        List<JSONObject> tagGroupList = new ArrayList<>();
        templateTagGroupMapping.forEach((key, value) -> {
            tagGroupList.add(value);
        });
        return tagGroupList;
    }


    /**
     * 获取员工可以看到的所有标签数据
     *
     * @param jsonObject
     * @return
     */
    private List<JSONObject> getUserTagList(Token token, JSONObject jsonObject) throws Exception {
        //1.模板标签 2.自定义标签 3.业务标签 4.第三方标签
        //模板标签组名称与模板标签组的映射关系
        Map<String, JSONObject> templateTagGroupMapping = new LinkedHashMap<>();

        JSONObject templateTagGroupCondition = new JSONObject();
        //模板标签组集合
        List<TemplateTag> templateTagGroupList = (List<TemplateTag>) templateTagService.groupList(templateTagGroupCondition);
        if (Objects.nonEmpty(templateTagGroupList)) {
            for (TemplateTag templateTagGroup : templateTagGroupList) {
                JSONObject templateTagGroupJson = (JSONObject) JSONObject.toJSON(templateTagGroup);
                templateTagGroupJson.put("type", TagSourceType.TEMPLATE_TAG.getId());
                templateTagGroupMapping.put(templateTagGroup.getName(), templateTagGroupJson);
            }
        }

        JSONObject thirdPartyTagGroupCondition = new JSONObject();
        //第三方标签组集合
        List<Tag> thirdPartyTagGroupList = (List<Tag>) thirdPartyTagService.groupList(thirdPartyTagGroupCondition);
        for (Tag thirdPartyTagGroup : thirdPartyTagGroupList) {
            if (templateTagGroupMapping.containsKey(thirdPartyTagGroup.getName())) {
                JSONObject templateTagGroupJson = templateTagGroupMapping.get(thirdPartyTagGroup.getName());
                JSONArray childList = templateTagGroupJson.getJSONArray("childList");
                //模板标签名称与模板标签名称的映射关系
                Map<String, String> templateTagMapping = new LinkedHashMap<>();
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagName = tag.getString("name");
                        templateTagMapping.put(tagName, tagName);
                    }
                }
                //如果自定义的标签与模板的标签一致则忽略自定义标签，否则在模板标签组中追加标签
                if (Objects.nonEmpty(thirdPartyTagGroup.getChildList())) {
                    thirdPartyTagGroup.getChildList().forEach(v -> {
                        if (!templateTagMapping.containsKey(v.getName())) {
                            JSONObject thirdPartyTagJson = (JSONObject) JSONObject.toJSON(v);
                            childList.add(thirdPartyTagJson);
                        }
                    });
                }

            } else {
                JSONObject thirdPartyGroupJson = (JSONObject) JSONObject.toJSON(thirdPartyTagGroup);
                thirdPartyGroupJson.put("type", TagSourceType.THIRD_PARTY_TAG.getId());
                templateTagGroupMapping.put(thirdPartyTagGroup.getName(), thirdPartyGroupJson);
            }

        }

        //过滤自己能看到的自定义标签数据(自己创建的,共创人是自己的)
        JSONObject customizeTagGroupCondition = new JSONObject();
        customizeTagGroupCondition.put("createBy", token.getUuid());
        List<CustomizeTag> customizeTagGroupList = (List<CustomizeTag>) customizeTagService.groupList(customizeTagGroupCondition);
        for (CustomizeTag customizeTagGroup : customizeTagGroupList) {
            if (templateTagGroupMapping.containsKey(customizeTagGroup.getName())) {
                JSONObject templateTagGroupJson = templateTagGroupMapping.get(customizeTagGroup.getName());
                JSONArray childList = templateTagGroupJson.getJSONArray("childList");
                //模板标签名称与模板标签名称的映射关系
                Map<String, String> templateTagMapping = new LinkedHashMap<>();
                if (Objects.nonEmpty(childList)) {
                    for (Object tagObj : childList) {
                        JSONObject tag = (JSONObject) tagObj;
                        String tagName = tag.getString("name");
                        templateTagMapping.put(tagName, tagName);
                    }
                }
                //如果自定义的标签与模板的标签一致则忽略自定义标签，否则在模板标签组中追加标签
                if (Objects.nonEmpty(customizeTagGroup.getChildList())) {
                    customizeTagGroup.getChildList().forEach(v -> {
                        if (!templateTagMapping.containsKey(v.getName())) {
                            JSONObject customizeTagJson = (JSONObject) JSONObject.toJSON(v);
                            childList.add(customizeTagJson);
                        }
                    });
                }

            } else {
                JSONObject customizeTagGroupJson = (JSONObject) JSONObject.toJSON(customizeTagGroup);
                customizeTagGroupJson.put("type", TagSourceType.CUSTOMIZE_TAG.getId());
                templateTagGroupMapping.put(customizeTagGroup.getName(), customizeTagGroupJson);
            }

        }

        List<JSONObject> tagGroupList = new ArrayList<>();
        templateTagGroupMapping.forEach((key, value) -> {
            tagGroupList.add(value);
        });
        return tagGroupList;
    }


}
